package com.example.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.admin.dao.AdChannelLabelMapper;
import com.example.admin.dao.AdChannelMapper;
import com.example.admin.dao.AdLabelMapper;
import com.example.admin.dao.AdSensitiveMapper;
import com.example.admin.service.AdChannelService;
import com.usian.model.admin.dtos.AdChannelQuery;
import com.usian.model.admin.dtos.ChannelDto;
import com.usian.model.admin.dtos.ReturnAdChannel;
import com.usian.model.admin.pojos.AdChannel;
import com.usian.model.admin.pojos.AdChannelLabel;
import com.usian.model.admin.pojos.AdLabel;
import com.usian.model.admin.pojos.AdSensitive;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.utils.common.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class AdServiceImpl implements AdChannelService {
    @Autowired
    private AdChannelMapper adChannelMapper;
    @Autowired
    private AdSensitiveMapper adSensitiveMapper;
    @Autowired
    private AdLabelMapper adLabelMapper;
    @Autowired
    private AdChannelLabelMapper adChannelLabelMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult addAdChannel(ChannelDto channelDto) {
        List<AdSensitive> sensitiveList = adSensitiveMapper.selectList(null);
        for (AdSensitive adSensitive : sensitiveList) {
            if (channelDto.getName().equals(adSensitive.getSensitives())){
                return ResponseResult.errorResult(201,"您添的频道名称不可用,请换一个试试");
            }
        }
        List<AdChannel> list = adChannelMapper.selectList(null);
        for (AdChannel adChannel : list) {
            if (adChannel.getName().equals(channelDto.getName())){
                return ResponseResult.errorResult(202,"频道名称重复,请换一个试试");
            }
        }
        AdLabel adLabel = new AdLabel();
        Boolean flag = false;
        List<AdLabel> adLabelList = adLabelMapper.selectList(null);
        for (AdLabel adLabel1 : adLabelList) {
            if (adLabel1.getName().equals(channelDto.getLabel())){
                adLabel.setId(adLabel1.getId());
                flag = true;
                break;
            }
        }
        if (!flag){
            adLabel.setId(Long.valueOf((SnowflakeIdWorker.UUID()+"").substring(8,13)));
            adLabel.setName(channelDto.getLabel());
            adLabel.setCreatedTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
            adLabel.setType(1);
            adLabelMapper.insert(adLabel);
        }
        AdChannel adChannel = new AdChannel();
        adChannel.setId(Integer.parseInt((SnowflakeIdWorker.UUID()+"").substring(0,5)));
        adChannel.setCreatedTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        adChannel.setDescription(channelDto.getDescription());
        adChannel.setIsDefault(false);
        adChannel.setName(channelDto.getName());
        adChannel.setOrd(2);
        if (channelDto.getStatus()==0){
            adChannel.setStatus(true);
        }else{
            adChannel.setStatus(false);
        }
        adChannelMapper.insert(adChannel);
        AdChannelLabel label = new AdChannelLabel();
        label.setChannelId(adChannel.getId());
        label.setLabelId(adLabel.getId());
        label.setId(Integer.parseInt((SnowflakeIdWorker.UUID()+"").substring(8,13)));
        label.setOrd(0);
        try {
            adChannelLabelMapper.insert(label);
            return ResponseResult.okResult(200,"添加频道成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.errorResult(500,"服务器内部错误");
        }
    }

    @Override
    public ResponseResult deleteChannelByName(String channelName) {
        QueryWrapper<AdChannel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",channelName);
        AdChannel adChannel = adChannelMapper.selectOne(queryWrapper);
        System.out.println(adChannel);
        if (adChannel!=null){
            if (adChannel.getStatus()){
                return ResponseResult.errorResult(400,"该频道正在使用,不能删除");
            }else{
                adChannel.setIsDefault(true);
                adChannelMapper.updateById(adChannel);
                return ResponseResult.okResult(200,"删除成功");
            }
        }
        return ResponseResult.errorResult(404,"该频道不存在");
    }

    @Override
    public ResponseResult queryAdChannel(AdChannelQuery adChannelQuery) {
        QueryWrapper<AdChannel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_default", 1);
        if ((adChannelQuery.getName().equals("") || adChannelQuery.getName() == null) && !adChannelQuery.getStatus()) {
            queryWrapper.eq("status", 0);
        } else if ((adChannelQuery.getName().equals("") || adChannelQuery.getName() == null) && adChannelQuery.getStatus()) {
            queryWrapper.eq("status", 1);
        } else if ((!adChannelQuery.getName().equals("") || adChannelQuery.getName() != null) && adChannelQuery.getStatus()) {
            queryWrapper.like("name", adChannelQuery.getName());
            queryWrapper.eq("status", 1);
        } else if ((!adChannelQuery.getName().equals("") || adChannelQuery.getName() != null) && !adChannelQuery.getStatus()) {
            queryWrapper.like("name", adChannelQuery.getName());
            queryWrapper.eq("status", 0);
        }
        if (adChannelQuery.getPageSize() == null || adChannelQuery.getPageSize() < 1) {
            adChannelQuery.setPageSize(10);
        }
        if (adChannelQuery.getPageNum() == null || adChannelQuery.getPageNum() < 1) {
            adChannelQuery.setPageNum(1);
        }
        IPage<AdChannel> page = new Page<>(adChannelQuery.getPageNum(), adChannelQuery.getPageSize());
        IPage<AdChannel> page1 = adChannelMapper.selectPage(page, queryWrapper);
        if (adChannelQuery.getPageNum() > page1.getPages()) {
            adChannelQuery.setPageNum(Integer.parseInt(page1.getPages() + ""));
            page = new Page<>(adChannelQuery.getPageNum(), adChannelQuery.getPageSize());
            page1 = adChannelMapper.selectPage(page, queryWrapper);
        }
        ReturnAdChannel returnAdChannel = new ReturnAdChannel();
        returnAdChannel.setPageNum(page1.getCurrent());
        returnAdChannel.setPageSize(page1.getPages());
        returnAdChannel.setList(page1.getRecords());
        returnAdChannel.setTotal(page1.getTotal());
        returnAdChannel.setPages(page1.getPages());
        return ResponseResult.okResult(returnAdChannel);
    }

    @Override
    public ResponseResult addSenticty(String name) {
        List<AdSensitive> adSensitives = adSensitiveMapper.selectList(null);
        for (AdSensitive adSensitive : adSensitives) {
            if (adSensitive.getSensitives().equals(name)){
                return ResponseResult.errorResult(400,"该敏感词已存在,请换一个试试");
            }
        }
        AdSensitive adSensitive = new AdSensitive();
        adSensitive.setId(Integer.parseInt((SnowflakeIdWorker.UUID()+"").substring(0,5)));
        adSensitive.setSensitives(name);
        adSensitive.setCreatedTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        try {
            adSensitiveMapper.insert(adSensitive);
            return ResponseResult.okResult(200,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.errorResult(500,"服务器内部错误");
        }
    }

    @Override
    public ResponseResult getAll() {
        return ResponseResult.okResult(adSensitiveMapper.selectList(null));
    }

    @Override
    public String getAdmin(Integer id) {
        LambdaQueryWrapper<AdChannel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AdChannel::getId,id);
        return adChannelMapper.selectOne(queryWrapper).getName();
    }

    public static void main(String[] args) {
        System.out.println((SnowflakeIdWorker.UUID()+"").substring(0,5));
    }
}
